package com.ccccat.www.msgs.Receiver;

import android.content.*;
import android.widget.*;

import com.ccccat.www.msgs.ConstData;
import com.ccccat.www.msgs.WsListener;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketFactory;

import java.io.IOException;

public class AlarmRecevier extends BroadcastReceiver { //声明一个振动器对象


    //  private String url = "ws://62.234.44.87:8088/websocket";
    private String url = "ws://192.168.123.237:8088/websocket";

    public static WsListener wsListener;
    public static WebSocket ws;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        //接受循环广播，启动界面
        //点亮亮屏
        if (intent.getAction().equals("sendbroadcast.action")) {

            SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
            String text_names = sharedPreferences.getString("text_name", "手机1");
            Integer text_gss = sharedPreferences.getInt("text_gs", 1);

            ConstData.AUTH_DATA.setName(text_names);
            ConstData.AUTH_DATA.setReadNum(text_gss);

            String name = ConstData.AUTH_DATA.getName();
            Integer readNum = ConstData.AUTH_DATA.getReadNum();


            if (wsListener == null || ws == null && (name != null || readNum != null)) {

                Toast.makeText(context, "重新连接中！", Toast.LENGTH_LONG).show();
                try {
                    ws = new WebSocketFactory().createSocket(url, 5000) //ws地址，和设置超时时间
                            .setFrameQueueSize(5)//设置帧队列最大值为5
                            .setMissingCloseFrameAllowed(false)//设置不允许服务端关闭连接却未发送关闭帧
                            .addListener(wsListener = new WsListener(context))//添加回调监听
                            .connectAsynchronously();//异步连接

                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "连接错误", Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(context, "检查服务都是正常的，没有毛病，放心用！", Toast.LENGTH_LONG).show();
            }


        }
    }
}
